/// @FUNCTION
/// @NAME XfMissionCreateMissionObject
/// @DESC 
/// @ACCESS Server
/// @MP False
/// @PARAM String The type of the object to create or an existing object
/// @PARAM Position The position where the object should be created. Can be a position or a combination of pos and dir: [[pos],dir]
/// @PARAM String The Object action type: REGISTER, DESTROY, ASSASSINATE, etc @DEFAULT "REGISTER"
/// @PARAM String The action parameters @DEFAULT []
/// @PARAM String The init line of the object @DEFAULT = ""
/// @PARAM String The special creation parameter: NONE, CAN_COLLIDE, etc. @DEFAULT = "CAN_COLLIDE"
/// @RETURN Object The created object
/// @REMARK Will sync with the ACV if it's present.
/// @EXAMPLE _obj = [["Land_Communication_F",getPos "Marker1"],"DESTROY",["Olaf's Money","You found the money","jip_money_found","Take the money",True]] call FFUNC(CreateMissionObject);

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "framework\fn_createMissionObject.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>
#include <ACV_constantes.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_obj_type","_obj_pos","_obj_dir","_framework_action","_obj_initline","_obj_special","_obj","_obj_name","_acv_params","_obj_params","_framework_params","_instance_id"];
DEFAULT_PARAM(_obj_params,TMF_CREATION_OBJPARAMS,"");
DEFAULT_PARAM(_framework_action,TMF_CREATION_ACTION,"REGISTER");
DEFAULT_PARAM(_framework_params,TMF_CREATION_ACTPARAMS,[]);
DEFAULT_PARAM(_instance_id,TMF_CREATION_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (_instance_id == TMF_SERVER_INSTANCE_ID && isMultiplayer && !isDedicated && !isnull player) then { _instance_id = getplayerUID player;};

_obj_type = 	GETVAL(_obj_params,TMF_VEH_PARAMS_TYPE,"");
_obj_pos = 		GETVAL(_obj_params,TMF_VEH_PARAMS_POS,ARR_3(0,0,0));
//_obj_group =	GETVAL(_obj_params,TMF_VEH_PARAMS_GRP,grpNull);
_obj_initline =	GETVAL(_obj_params,TMF_VEH_PARAMS_INIT,[]);
_obj_special = 	GETVAL(_obj_params,TMF_VEH_PARAMS_SPEC,"NONE");

// Case where we receive a marker as the position
if (typename _obj_pos == typename "") then {
	_obj_dir = markerDir _obj_pos;
	_obj_pos = markerPos _obj_pos;
};
// Case where we receive the position plus the direction.
if (typename (_obj_pos select 0) == typename []) then {
	_obj_dir = _obj_pos select 1;
	_obj_pos = _obj_pos select 0;
};	

_obj = objNull;	
DEBUG_1("Creation Params",ARR_6(_obj_type,_obj_pos,_framework_action,_framework_params,_obj_initline,_obj_special));

// Object position correction
if ((_obj_pos select 2) < 0) then {_obj_pos set [2,0]};

// Checks if the object already exists
If (typeName _obj_type == typename objNull) then {
	_obj = _obj_type;
	_obj_type = typeOf _obj;
} else {
	// Creating using the mission framework
	_obj = createVehicle [_obj_type, _obj_pos, [], 0, _obj_special];
	_obj setPos _obj_pos;
	if (!isNil "_obj_dir") then {_obj setDir _obj_dir;}; 

	if (!isnil "ACV_FNCT_PutInStock") then {
		_obj_name = _obj_type;
		if (typename (_framework_params) == typename []) then {
			if (count _framework_params > 0) then {
				if ((typename (_framework_params select 0) == typename "") AND (_framework_action == "PICKUP")) then {_obj_name = _framework_params select 0;};
			};
		};
		_type = 0;
		_type = ACV_NET_MAKESIMPLEOBJECT;
		[_type, _obj_group, _obj_name, _instance_id] call ACV_FNCT_PutInStock;
		[ format[localize "STR_MSG_CREATEDBY",_obj_name, _instance_id]] call ACV_FNCT_BroadcastMessage;
	};
};

// We set the action and parameters for the registration
[_obj,_instance_id] call FFUNC(setInstanceID);
_action = call CFUNC(hashEmpty);
_action = [_action, (call CFUNC(nextJipID))] call FFUNC(actionSetID);
_action = [_action, _framework_action] call FFUNC(actionSetType);
_action = [_action, _framework_params] call FFUNC(actionSetParams);
_action = [_action, _obj_params] call FFUNC(actionSetObjParams);
_action = [_action, _instance_id] call FFUNC(actionSetInstance);
[_obj,_action] call FFUNC(actionSet);

// We register the object on all systems
[QGVAR(all_object),[_obj,_action],THIS_FILE_] call NFUNC(callEvent);

TRACE_1("End",ARR_2(_obj, typeof _obj));
TMFPROFILERSTOP;
// We return the object.
_obj;
